package com.guigu.dispatch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.guigu.dispatch.entity.CacheLineUse;
import com.guigu.dispatch.entity.OrderClassifyAttach;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dispatch.entity.OrderClassifyAttachDTO;
import com.guigu.dispatch.mapper.OrderClassifyAttachMapper;
import com.guigu.dispatch.service.CacheLineUseService;
import com.guigu.dispatch.service.OrderClassifyAttachService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.work.service.TransportOrderTaskService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 车次车辆司机订单分类关联表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-18
 */
@Service
@Transactional
public class OrderClassifyAttachServiceImpl extends ServiceImpl<OrderClassifyAttachMapper, OrderClassifyAttach> implements OrderClassifyAttachService {

    @Autowired
    private CacheLineUseService cacheLineUseService;


    /**
     * 添加订单分类车辆司机车次关联表以及订单分类和线路的中间表
     *
     * @param dto
     * @return
     */
    @Override
    public R orderClassifyTripsTruckDriverAdd(OrderClassifyAttachDTO dto) {
        if (ObjectUtils.isEmpty(dto)) {
            throw new TmsException(ResultCode.ERROR_CODE, "对象dto为空!");
        }

        OrderClassifyAttach attach = new OrderClassifyAttach();
        CacheLineUse cacheLineUse = new CacheLineUse();
        BeanUtil.copyProperties(dto, attach);
        BeanUtil.copyProperties(dto, cacheLineUse);
        attach.setCreateDate(new Date());
        cacheLineUse.setCreateDate(new Date());
        if (ObjectUtils.isEmpty(attach) || ObjectUtils.isEmpty(cacheLineUse)) {
            throw new TmsException(ResultCode.ERROR_CODE, "添加失败!");
        }
        /**
         * 先删除之前有的,在添加
         */
        QueryWrapper<OrderClassifyAttach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trips_id", attach.getTripsId());
        queryWrapper.eq("truck_id", attach.getTruckId());
        queryWrapper.eq("driver_id", attach.getDriverId());
        queryWrapper.eq("order_classify_id", attach.getOrderClassifyId());
        this.baseMapper.delete(queryWrapper);
        QueryWrapper<CacheLineUse> q = new QueryWrapper<>();
        q.eq("cache_line_id", cacheLineUse.getCacheLineId());
        q.eq("order_classify_id", cacheLineUse.getOrderClassifyId());
        this.cacheLineUseService.remove(q);

        /**
         * 删除之前关联度的数据
         */


        /**
         * 添加
         */
        this.baseMapper.insert(attach);
        this.cacheLineUseService.save(cacheLineUse);
        return new R(ResultCode.SUCCESS_CODE, "分配车次车辆司机成功!", true);
    }
}
